package me.zhengjie.modules.lock.service.impl;

import com.zelk.lock.api.clent.feign.LockDeviceLogApi;
import com.zelk.lock.api.dto.LockDeviceLogListDTO;
import com.zelk.lock.api.vo.LockDeviceLogPageVO;
import com.zelk.smartlock.common.response.ResponsePage;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.lock.service.LockDeviceLogService;
import me.zhengjie.utils.PageUtil;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class LockDeviceLogServiceImpl implements LockDeviceLogService {

    @Resource
    private LockDeviceLogApi lockDeviceLogApi;

    @Override
    public Map<String, Object> getLogs(LockDeviceLogPageVO query, Pageable pageable) {
        query.setPageIdx((long) pageable.getPageNumber());
        query.setPageSize(pageable.getPageSize() <= 0 ? 50L : (long) pageable.getPageSize());
        ResponsePage<List<LockDeviceLogListDTO>> deviceLogApiLogs = lockDeviceLogApi.getLogs(query);
        List<LockDeviceLogListDTO> lockDeviceLogListDTOS = Optional.ofNullable(deviceLogApiLogs.getData()).orElse(new ArrayList<>());
        return PageUtil.toPage(lockDeviceLogListDTOS, Objects.nonNull(deviceLogApiLogs.getTotal()) ? deviceLogApiLogs.getTotal() : 0);
    }
}
